package com.youlu.campus.service.activity;

import com.youlu.campus.base.exception.BusinessException;
import com.youlu.campus.entity.ActivityInfo;
import com.youlu.campus.entity.ActivityQaInfo;
import com.youlu.campus.entity.MerchantSharingProfitInfo;
import com.youlu.campus.entity.VO.req.ActivityCopyReq;
import com.youlu.campus.entity.system.UserDataPermission;
import com.youlu.campus.service.activity.impl.ActivityPayConfigService;
import com.youlu.campus.service.ca.CaGroupInfoService;
import com.youlu.campus.service.ca.CaMaterialConfigService;
import com.youlu.campus.service.ca.impl.CertificateActivityConfigService;
import com.youlu.campus.service.data.impl.UvPvService;
import com.youlu.campus.service.qa.ActivityQaInfoService;
import com.youlu.campus.service.system.UserDataPermissionService;
import lombok.extern.slf4j.Slf4j;
import ma.glasnost.orika.MapperFacade;
import org.apache.commons.collections4.CollectionUtils;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class ActivityCopyService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private MapperFacade mapperFacade;

    @Autowired
    private ActivityTaskService activityTaskService;

    @Autowired
    private ActivityQaInfoService activityQaInfoService;

    @Autowired
    private CertificateActivityConfigService certificateActivityConfigService;

    @Autowired
    private CaGroupInfoService caGroupInfoService;

    @Autowired
    private ActivityPayConfigService activityPayConfigService;

    @Autowired
    private UvPvService uvPvService;

    @Autowired
    private UserDataPermissionService userDataPermissionService;

    @Autowired
    private CaMaterialConfigService caMaterialConfigService;


    public void doCopy(String userId, String name, String activityId) {
        log.info("【copy 活动】start userId->{} | activityId->{}", userId, activityId);
        ActivityInfo copyActivityInfo = mongoTemplate.findById(activityId, ActivityInfo.class);
        if ("wx-mini-dreambeat-contest".equals(copyActivityInfo.getType()) || "before".equals(copyActivityInfo.getType())) {
            String copyActivityName = copyActivityInfo.getName() + "-Copy-" + name + System.currentTimeMillis();
            String newActivityId = ObjectId.get().toString();
            Date now = new Date();
            log.info("【copy 活动】开始复制活动对象");
            ActivityInfo copyTargetActivity = mapperFacade.map(copyActivityInfo, ActivityInfo.class);
            copyTargetActivity.setId(newActivityId);
            copyTargetActivity.setName(copyActivityName);
            copyTargetActivity.setCreatedTime(now);
            copyTargetActivity.setUpdatedTime(now);
            copyTargetActivity.setCreatorId(userId);
            MerchantSharingProfitInfo merchantSharingProfit = copyTargetActivity.getMerchantSharingProfit();
            if (Objects.nonNull(merchantSharingProfit)) {
                copyTargetActivity.setMerchantSharingProfit(null);
            }
            this.mongoTemplate.save(copyTargetActivity);
            UserDataPermission userDataPermission = userDataPermissionService.findOne(userId);
            if (Objects.nonNull(userDataPermission)) {
                List<String> activityIdDataList = userDataPermission.getActivityId();
                if (CollectionUtils.isEmpty(activityIdDataList)) {
                    activityIdDataList = new ArrayList<>();
                }
                activityIdDataList.add(newActivityId);
                userDataPermission.setActivityId(activityIdDataList);

                List<ActivityInfo> activityInfos = userDataPermission.getActivityInfos();
                if (CollectionUtils.isEmpty(activityInfos)) {
                    activityInfos = new ArrayList<>();
                }
                activityInfos.add(copyActivityInfo);
                userDataPermission.setActivityInfos(activityInfos);
                this.mongoTemplate.save(userDataPermission);
            }
            log.info("【copy 活动】结束复制活动对象");
            log.info("【copy 活动】==============================================");
            log.info("【copy 活动】开始复制活动任务及任务规则");
            ActivityCopyReq activityCopyReq = new ActivityCopyReq();
            activityCopyReq.setCopyActivityId(copyActivityInfo.getId());
            activityCopyReq.setTargetCopyActivityId(newActivityId);
            activityCopyReq.setTargetCopyActivityName(copyActivityName);
            activityTaskService.doCopyTask(activityCopyReq);
            log.info("【copy 活动】结束复制活动任务及任务规则");
            log.info("【copy 活动】==============================================");
            log.info("【copy 活动】开始复制活动题库规则");
            activityQaInfoService.doCopyQaInfo(activityId, newActivityId, copyActivityName);
            log.info("【copy 活动】结束复制活动题库规则");
            log.info("【copy 活动】==============================================");
            log.info("【copy 活动】开始活动证书规则");
            if ("wx-mini-dreambeat-contest".equals(copyActivityInfo.getType())) {
                certificateActivityConfigService.doCopyCertificateConfig(activityId, newActivityId);
                log.info("【copy 活动】结束活动证书规则");
                log.info("【copy 活动】==============================================");
                log.info("【copy 活动】开始复制证书序列配置");
                caGroupInfoService.copyCaGroupInfo(activityId, newActivityId);
                log.info("【copy 活动】结束复制证书序列配置");
                log.info("【copy 活动】==============================================");
                log.info("【copy 活动】开始复制活动收款配置");
                activityPayConfigService.doCopyPayConfig(activityId, newActivityId);
                log.info("【copy 活动】结束复制活动收款配置");
                log.info("【copy 活动】==============================================");
                log.info("【copy 活动】开始复制供应链材质配置");
                caMaterialConfigService.doCopyCaMaterialConfig(activityId, copyActivityName, newActivityId);
                log.info("【copy 活动】结束复制供应链材质配置");
                log.info("【copy 活动】==============================================");
                log.info("【copy 活动】end userId->{} | activityId->{}", userId, activityId);
            }

            try {
                //同步活动信息到统计系统
                ActivityQaInfo activityQaInfo = new ActivityQaInfo();
                activityQaInfo.setActivityId(newActivityId);
                activityQaInfo.setActivityName(copyActivityName);
                activityQaInfo.setStartTime(copyActivityInfo.getStartTime());
                activityQaInfo.setEndTime(copyActivityInfo.getEndTime());
                uvPvService.doActivityInfo(activityQaInfo);
            } catch (Exception e) {
                log.error("同步活动信息错误", e);
            }

        }
    }
}
